﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace EmailManager.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmailController : ControllerBase
    {
        [HttpPost("")]
        public ActionResult Index([FromForm] EmailForm form)
        {
            var (email, content, topic) = form;
            var to = new List<string> { email };
            content = Config.ContentBefore + content + Config.ContentAfter;
            EmailSender.SendEmail(to, topic, content);
            return Ok("成功");
        }
    }
    
    public class EmailForm
    {
        [Required]
        public string Email { get; set; }
        [Required]
        public string Content { get; set; }
        [Required]
        public string Topic { get; set; }

        public void Deconstruct(out string email, out string content, out string topic)
        {
            email = Email;
            content = Content;
            topic = Topic;
        }
    }
}
